package com.jyserver.common.AutoCode.jybaits.element.pc.From;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jyserver.common.AutoCode.jybaits.element.pc.Rules;
import com.jyserver.common.utils.HelpUtils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class View {


    private List<String> imports = new ArrayList<String>();

    private List<String> rulelist = new ArrayList<String>();

    private List<String> forms = new ArrayList<String>();

    private List<String> components = new ArrayList<String>();

    private List<String> datas = new ArrayList<String>();

    private String apikey = "key";

    private String apilabel = "note";

    private List<String> validatorList = new ArrayList<String>();

    private Map jsonRules = (Map) JSONObject.parse(JSON.toJSONString(new Rules()));

    private List<String> formitems = new ArrayList<String>();

    private String postapi = null;

    private String header = "<template>\n" +
            "  <div>\n" +
            "    <el-row type=\"flex\" class=\"row-bg\" justify=\"center\">\n" +
            "      <el-col :xs=\"24\" :sm=\"22\" :md=\"20\" :lg=\"20\" :xl=\"19\" >";
    private String endString = "</el-col>\n" +
            "    </el-row>\n" +
            "  </div>\n" +
            "</template>";

    public String getPostapi() {
        return postapi;
    }

    public void setPostapi(String postapi) {
        this.postapi = postapi;
    }

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public String getEndString() {
        return endString;
    }

    public void setEndString(String endString) {
        this.endString = endString;
    }

    public View() {

    }
    public View(Object value) {
        FormView(value);
    }

    public List<String> getImports() {
        return imports;
    }

    public void setImports(List<String> imports) {
        this.imports = imports;
    }

    public String getApikey() {
        return apikey;
    }

    public void setApikey(String apikey) {
        this.apikey = apikey;
    }

    public String getApilabel() {
        return apilabel;
    }

    public void setApilabel(String apilabel) {
        this.apilabel = apilabel;
    }

    public void addformitems(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            formitems.add(str);
        }
    }

    public void addcomponents(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            components.add(str);
        }
    }

    public String getcomponents() {
        if (components.size() == 0) {
            return "";
        }
        String str = "components: {\n";
        for (int i = 0; i < components.size(); i++) {
            str = str + components.get(i) + ",\n";
        }
        return str + "},\n";
    }

    public void adddatas(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            datas.add(str);
        }
    }

    public String getdataString() {
        if (datas.size() == 0) {
            return "";
        }
        String str = "";
        for (int i = 0; i < datas.size(); i++) {
            str = str + datas.get(i) + ",\n";
        }
        return str;
    }


    public String getformitems() {
        String str = "";
        for (int i = 0; i < formitems.size(); i++) {
            str = str + formitems.get(i);
        }
        return str;
    }

    public void addimport(String import_son) {
        if (HelpUtils.isNotEmpty(import_son)) {
            imports.add(import_son);
        }
    }

    public String getimport() {
        String str = "";
        for (int i = 0; i < imports.size(); i++) {
            str = str + imports.get(i) + "\n";
        }
        return str;
    }

    public void addforms(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            forms.add(str);
        }
    }

    public void addrules(String str) {
        if (HelpUtils.isNotEmpty(str)) {
            rulelist.add(String.valueOf(str));
        }
    }

    public String getStringRules() {
        String str = "\t\t\trules: {\n";
        for (int i = 0; i < rulelist.size(); i++) {
            str = str + "\t\t\t\t" + rulelist.get(i);
        }
        return str + "\t\t\t},\n";
    }

    public String getFormsString() {
        String str = "\t\t\tViewform: {\n";
        for (int i = 0; i < forms.size(); i++) {
            str = str + "\t\t\t\t" + forms.get(i) + ":'',\n";
        }
        return str + "\t\t\t},\n";
    }

    public String getValidatorString() {
        if (validatorList.size() == 0) {
            return "";
        }
        String str = "";
        for (int i = 0; i < validatorList.size(); i++) {
            str = str + validatorList.get(i);
        }
        return str;
    }

    public String gtscript() {
        String str = "\n<script>\n" + getimport() + "export default {\n" + getcomponents() + getdata() + beforeCreate() + methods();
        return str + "\n}\n</script>\n";
    }

    public String getdata() {
        String str = "\ndata() {\n " + getValidatorString() + "return {\nformLabelWidth: \"150px\",\n" + getFormsString() + getStringRules() + getdataString();
        return str + "\n}\n},\n";
    }

    public String methods() {
        String str = "\nmethods: {\n" + submitForm() + resetForm();
        return str + "\n},\n";
    }





    public String beforeCreate() {
        String str = "\nbeforeCreate(){\n";
        return str + "\n},\n";
    }

    public String submitForm() {
        String str = null;

        try {
            str = "submitForm(form) {\n" +
                    "        this.$refs[form].validate((valid) => {\n" +
                    "          if (valid) {\n" + " let a = new URLSearchParams();\n" +
                    "            for (let key in this.Viewform) {\n" +
                    "              a.append(key, this.Viewform[key])\n" +
                    "            }\n" +
                    "            this.axios.post('" + postapi.replace("http://d.huayuboneng.com", "") + "', a)\n" +
                    "              .then((response) => {\n" +
                    "                if (response.data.success) {\n" +
                    "                  this.$message({\n" +
                    "                    message: '" + (new String("请求成功".getBytes("ISO-8859-1"), "UTF-8")) + "',\n" +
                    "                    type: 'success'\n" +
                    "                  });\n" +
                    "                }\n" +
                    "              });\n" +
                    "          } else {\n" +
                    "            console.log('error submit!!');\n" +
                    "            return false;\n" +
                    "          }\n" +
                    "        });\n" +
                    "      },\n";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    public String resetForm() {
        String str = "resetForm(formName) {\n" +
                "        this.$refs[formName].resetFields();\n" +
                "      },\n";
        return str;
    }

    public String FormView(Object value, String key1, String key2, Object api) {
        JSONArray array = JSONObject.parseArray(String.valueOf(value));
        postapi = String.valueOf(api);
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObjectSon = JSONObject.parseObject(String.valueOf(array.get(i)));
            addforms(String.valueOf(jsonObjectSon.get(apikey)));
            String tems = "";
            String prop = String.valueOf(jsonObjectSon.get(apikey));
            JSONArray rules_array = JSONObject.parseArray(String.valueOf(jsonObjectSon.get("rules")));
            JSONArray validate_array = JSONObject.parseArray(String.valueOf(jsonObjectSon.get("validators")));
            addformitems(Input.getStyleView(String.valueOf(jsonObjectSon.get(apilabel)), prop, String.valueOf(jsonObjectSon.get(apikey))));
        }
        return  header+getformitems()+endString;
    }


    public void FormView(Object value) {
        JSONArray array = JSONObject.parseArray(String.valueOf(value));
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObjectSon = JSONObject.parseObject(String.valueOf(array.get(i)));
            addforms(String.valueOf(jsonObjectSon.get(apikey)));
            String tems = "";
            String prop = String.valueOf(jsonObjectSon.get(apikey));
            JSONArray rules_array = JSONObject.parseArray(String.valueOf(jsonObjectSon.get("rules")));
            JSONArray validate_array = JSONObject.parseArray(String.valueOf(jsonObjectSon.get("validators")));
            addformitems(Input.getStyleView(String.valueOf(jsonObjectSon.get(apilabel)), prop, String.valueOf(jsonObjectSon.get(apikey))));
        }
    }


    public String getform() {
        return header + " <el-form :model=\"Viewform\" :rules=\"rules\" ref=\"Viewform\" size=\"mini\">" + getformitems() + "</el-form>" + endString;
    }

    public String test(Object value, String key1, String key2, Object api) {
        JSONArray array = JSONObject.parseArray(String.valueOf(value));
        postapi = String.valueOf(api);
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObjectSon = JSONObject.parseObject(String.valueOf(array.get(i)));
            addforms(String.valueOf(jsonObjectSon.get(apikey)));
            String tems = "";
            String prop = String.valueOf(jsonObjectSon.get(apikey));
            JSONArray rules_array = JSONObject.parseArray(String.valueOf(jsonObjectSon.get("rules")));
            JSONArray validate_array = JSONObject.parseArray(String.valueOf(jsonObjectSon.get("validators")));
            if ("select".equals(jsonObjectSon.get("fromtye"))) {
                tems = Select.getStyle(String.valueOf(jsonObjectSon.get(apilabel)), prop, String.valueOf(jsonObjectSon.get(apikey)), JSONObject.parseArray(String.valueOf(jsonObjectSon.get("optionsArray"))));
            } else if ("checkbox".equals(jsonObjectSon.get("fromtye"))) {
                tems = Checkbox.getStyle(String.valueOf(jsonObjectSon.get(apilabel)), prop, String.valueOf(jsonObjectSon.get(apikey)), JSONObject.parseArray(String.valueOf(jsonObjectSon.get("checkboxsArray"))));
            } else if ("Uploader".equals(jsonObjectSon.get("fromtye"))) {
                tems = Uploader.getStyle(String.valueOf(jsonObjectSon.get(apilabel)));
                if (!components.contains("myuploader")) {
                    imports.add("import myuploader from \"@//components/myuploader\";");
                    components.add("myuploader");
                }
            } else if ("automaticuploader".equals(jsonObjectSon.get("fromtye"))) {
                tems = Uploader.getautomaticuploader(String.valueOf(jsonObjectSon.get(apilabel)), prop, String.valueOf(jsonObjectSon.get(apikey)));
                imports.add("import automaticuploader from \"@//components/automaticuploader\";");
                imports.add("import {mapState} from 'vuex'");
                components.add("automaticuploader");
                datas.add("baseurl: 'http://d.huayuboneng.com/'");
            } else {
                tems = Input.getStyle(String.valueOf(jsonObjectSon.get(apilabel)), prop, String.valueOf(jsonObjectSon.get(apikey)));
                //                tems=Input.getStyleView(String.valueOf(jsonObjectSon.get(apilabel)), prop, String.valueOf(jsonObjectSon.get(apikey)));
            }

            addformitems(tems);
        }

        String string = null;
        try {
            string = header + " <el-form :model=\"Viewform\" :rules=\"rules\" ref=\"Viewform\" size=\"mini\">" + getformitems() + "<el-form-item>\n" +
                    "    <el-button type=\"primary\" @click=\"submitForm('Viewform')\">" + (new String("提交".getBytes("ISO-8859-1"), "UTF-8")) + "</el-button>\n" +
                    "    <el-button @click=\"resetForm('Viewform')\">" + (new String("重置".getBytes("ISO-8859-1"), "UTF-8")) + "</el-button>\n" +
                    "  </el-form-item>" + "</el-form>" + endString;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return string + gtscript() + "<style scoped>\n</style>";
    }

}
